package net.huashitong.user.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.huashitong.user.databean.DynamicDatabean;
import net.huashitong.user.entity.Dynamic;
import net.huashitong.user.entity.DynamicUser;
import net.huashitong.user.filter.DynamicFilter;
import net.huashitong.user.service.DynamicService;
import net.huashitong.user.service.DynamicUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;

/**
 * 消息动态
 * @author lzr
 *
 */
@Controller
@RequestMapping("/app/dynamic")
public class DynamicController extends JsonBaseController {

	@Autowired
	private DynamicService dynamicService;
	@Autowired
	private  DynamicUserService dynamicUserService;
	@Override
	protected String getView(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 保存消息动态
	 * @param dynamicFilter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/saveDynamic")
	public Message saveDynamic(@RequestBody DynamicFilter dynamicFilter){
		
		Dynamic dynamic = new Dynamic();
		dynamic.setUserId(dynamicFilter.getUserId());
		dynamic.setPhotoId(dynamicFilter.getPhotoId());
		dynamic.setText(dynamicFilter.getText());
		dynamic.setDate(dynamicFilter.getDate());
		dynamic.setWrittenBy(dynamicFilter.getWrittenBy());
		dynamic.setSubject(dynamicFilter.getSubject());
		dynamic.setType(dynamicFilter.getType());
		dynamicService.save(dynamic);
		return coverMessage("200", "保存成功");
	}
	/**
	 * 查询消息动态
	 * @return
	 */
	@RequestMapping("/queryDynamic")
	@ResponseBody
	public Message queryDynamic(@RequestBody DynamicFilter dynamicFilter){
		List<Dynamic> dynamics = dynamicService.queryByType(dynamicFilter);
		List<DynamicDatabean> databeans = new ArrayList<DynamicDatabean>();
		if(ObjectUtils.isNotBlank(dynamics)){
		for (Dynamic dynamic : dynamics) {
			DynamicDatabean databean = new DynamicDatabean();
			databean.setDate(dynamic.getDate());
			databean.setPhotoId(dynamic.getPhotoId());
			databean.setText(dynamic.getText());
			databean.setSysId(dynamic.getSysId());
			databean.setWrittenBy(dynamic.getWrittenBy());
			databean.setSubject(dynamic.getSubject());
			databean.setType(dynamic.getType());
			DynamicUser du=dynamicUserService.queryByUserIdAndDYnamcId(dynamicFilter.getUserId(),dynamic.getSysId());
			if(ObjectUtils.isBlank(du)){
				databean.setState("0");
			}else{
				databean.setState("1");
			}
			databeans.add(databean);
		}
		}
		return coverMessage("200", "数据获取成功",databeans);
	}
	
	@RequestMapping("/delDynamic")
	@ResponseBody
	public Message delDynamic(@RequestBody DynamicFilter dynamicFilter){
		dynamicService.deleteById(dynamicFilter.getSysId());
		
		return coverMessage("200", "数据删除成功");
	}
	@RequestMapping("/updateDynamic")
	@ResponseBody
	public Message updateDynamic(@RequestBody DynamicFilter dynamicFilter){
		DynamicUser du=new DynamicUser();
		du.setDynamicId(dynamicFilter.getSysId());
		du.setUserId(dynamicFilter.getUserId());
		dynamicUserService.save(du);
		return coverMessage("200", "数据更新成功");
	}
	
	@RequestMapping("/queryDynamicStateList")
	@ResponseBody
	public Message queryDynamicStateList(@RequestBody DynamicFilter dynamicFilter){
		dynamicFilter.setState("0");
		int ac=0;
		int bc=0;
		int cc=0;
		int dc=0;
				
		List<Dynamic> dynamics=dynamicService.query();
	    List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
			if(ObjectUtils.isNotBlank(dynamics)){
				for(Dynamic dy:dynamics){
					DynamicUser du=dynamicUserService.queryByUserIdAndDYnamcId(dynamicFilter.getUserId(),dy.getSysId());
					if(ObjectUtils.isBlank(du)){
					if("领导批示".equals(dy.getType())){
						ac=ac+1;
					}else if("会议通知".equals(dy.getType())){
						bc=bc+1;
					}else if("工作动态".equals(dy.getType())){
						cc=cc+1;
					}else if("表扬与批评".equals(dy.getType())){
						dc=dc+1;
					}
				  } 	
				}
		    }
	   Map<String, Object> map1= new HashMap<String, Object>();
	   map1.put("type","领导批示");
	   map1.put("number", ac);
	   Map<String, Object> map2= new HashMap<String, Object>();
	   map2.put("type","会议通知");
	   map2.put("number", bc);
	   Map<String, Object> map3= new HashMap<String, Object>();
	   map3.put("type","工作动态");
	   map3.put("number", cc);
	   Map<String, Object> map4= new HashMap<String, Object>();
	   map4.put("type","表扬与批评");
	   map4.put("number", dc);
	   list.add(map1);
	   list.add(map2);
	   list.add(map3);
	   list.add(map4);
	   return coverMessage("200", "获取数据成功",list);
	}
}
